home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / Orm.lha / Orm / RCS / Orm.c,v < prev   
Text File  |  1993-12-22  |  14KB  |  698 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.4
  10. date     93.10.21.16.21.23;  author Torsten;  state Rel;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     93.10.19.18.05.15;  author Torsten;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     93.10.12.18.03.27;  author Torsten;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     93.10.12.00.04.41;  author Torsten;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @Michael Warner's PUBLIC DOMAIN version of the classic game SNAKE.
  32. @
  33.  
  34.  
  35. 1.4
  36. log
  37. @Pause key changed from 'P' to spacebar.
  38. @
  39. text
  40. @/*
  41.  * This is a modified version of Michael Warner's
  42.  * Public Domain Workbench SNAKE game.
  43.  *
  44.  * This (improved?) version by Torsten Poulin
  45.  * is, of course, still in the public domain.
  46.  *
  47.  *   Torsten Poulin
  48.  *   Banebrinken 99, 2, 77
  49.  *   DK-2400 Copenhagen NV
  50.  *   DENMARK
  51.  *
  52.  * THIS IS A WB 2+ PROGRAM!!!
  53.  *
  54.  * $Log:    Orm.c,v $
  55.  * Revision 1.3  93/10/19  18:05:15  Torsten
  56.  * Localized. Defaults to builtin Danish strings if
  57.  * locale.library isn't available; thus, it still runs
  58.  * under AmigaDOS 2.04 (V37).
  59.  * 
  60.  * Revision 1.2  93/10/12  18:03:27  Torsten
  61.  * Added function prototypes.
  62.  * Can be compiled with either DICE or SAS/C 5.10.
  63.  * Tries to be smart about the screen display mode.
  64.  * Obstacles disabled on two-colour screens.  
  65.  * Knows about window border sizes.
  66.  * 
  67.  * Revision 1.1  93/10/12  00:04:41  Torsten
  68.  * This is my first version of Michael's game.
  69.  * 
  70.  */
  71.  
  72. char RCSid[] = "$Id: Orm.c,v 1.3 93/10/19 18:05:15 Torsten Exp Locker: Torsten $";
  73. char version[] = "$VER: Orm 1.4 (21.10.93)";
  74. char copyright[] = "$COPYRIGHT:*PUBLIC DOMAIN* - M. Warner & T. Poulin$";
  75.  
  76. #include <exec/types.h>
  77. #include <intuition/intuition.h>
  78. #include <libraries/locale.h>
  79. #include <clib/exec_protos.h>
  80. #include <clib/intuition_protos.h>
  81. #include <clib/graphics_protos.h>
  82. #include <clib/dos_protos.h>
  83. #include <clib/locale_protos.h>
  84. #include <stdlib.h>
  85. #include <stdio.h>
  86. #include <time.h>
  87. #include <sys/types.h>
  88. #include "orm.h"
  89.  
  90. /*
  91.  * Compiler dependencies
  92.  */
  93.  
  94. #if defined(__SASC) | defined(LATTICE) | defined(_DCC)
  95.  
  96. #ifdef _DCC
  97. void fool_DICE(void) { _waitwbmsg(); }
  98. #define main _main
  99. #define __far
  100. #define __stdargs
  101.  
  102. #else /* __SASC | LATTICE */
  103. #ifdef __SASC_510
  104. #include <pragmas/exec_pragmas.h>
  105. #include <pragmas/intuition_pragmas.h>
  106. #include <pragmas/graphics_pragmas.h>
  107. #include <pragmas/locale_pragmas.h>
  108. extern struct DosLibrary *DOSBase;
  109. #include <pragmas/dos_pragmas.h>
  110. #endif /* __SASC_510 */
  111. void main(void);
  112. void _main(char *dummy) { main(); }
  113. #endif /* __SASC | LATTICE */
  114.  
  115. #else /* something else */
  116. #define __far
  117. #define __stdargs
  118. #endif
  119.  
  120. #define XSIZE 40
  121. #define YSIZE 40
  122. #define BSIZE 3
  123. #define NFROGS 4
  124. #define PRIORITY 0
  125.  
  126. #define CLEAR 1
  127. #define SNAKE 0
  128. #define BRICK 3
  129. #define FROG  2
  130.  
  131. #define KEY_ESC   69
  132. #define KEY_Pause 64
  133. #define KEY_UP    76
  134. #define KEY_DOWN  77
  135. #define KEY_RIGHT 78
  136. #define KEY_LEFT  79
  137.  
  138. extern UWORD __stdargs RangeRand(ULONG maxValue);
  139. void exit_prog(int num);
  140. void open_window(void);
  141. void close_window(void);
  142. void notify(char *str);
  143. void set_point(int x, int y, int item);
  144. void clear_grid(void);
  145. void draw_score(void);
  146. void pause(void);
  147. void create_frog(int n);
  148. void check_frogs(void);
  149. void replace_frog(int x, int y);
  150. void setup_game(void);
  151. BOOL play_one_game(void);
  152. void play_game(void);
  153. void randomize(void);
  154. char *getcatalogstr(struct Catalog *, LONG, char *);
  155.  
  156. APTR IntuitionBase, GfxBase;
  157. struct LocaleBase *LocaleBase;
  158. struct Catalog *catalog;
  159. struct Window *win;
  160. struct RastPort *rp;
  161. LONG xstretch, ystretch;
  162. LONG border_left, border_top;
  163.  
  164. UBYTE grid[XSIZE][YSIZE];
  165. UBYTE snx[1500], sny[1500], frx[NFROGS], fry[NFROGS];
  166. int dir, length, head, tail, grow, frogtime[NFROGS];
  167. UBYTE key;
  168. BOOL has_obsta = FALSE;
  169.  
  170.  
  171. void exit_prog(int num)
  172. {
  173.   if (LocaleBase) CloseLibrary((struct Library *) LocaleBase);
  174.   if (GfxBase) CloseLibrary((struct Library *) GfxBase);
  175.   if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);
  176.   exit(num);
  177. }
  178.  
  179.  
  180. void open_window(void)
  181. {
  182.   struct Screen *scr;
  183.   struct DrawInfo *scr_di;
  184.   char *title;
  185.  
  186.   if (!(scr = LockPubScreen(NULL)))
  187.     exit_prog(20);
  188.  
  189.   if (scr_di = GetScreenDrawInfo(scr)) {
  190.     has_obsta = scr_di->dri_Depth > 1;
  191.     FreeScreenDrawInfo(scr, scr_di);
  192.   }
  193.  
  194.   /*
  195.    * Determine stretch factors heuristically.
  196.    * Please note that this is a hack and the result
  197.    * will not be pleasing in the VGA-ExtraLores modes :-(
  198.    */
  199.   if (scr->Width <= 500)
  200.     xstretch = 1; /* Lores */
  201.   else if (scr->Width <= 800)
  202.     xstretch = 2; /* Hires */
  203.   else
  204.     xstretch = 4; /* SuperHires or A2024 */
  205.  
  206.   if (scr->Height <= 300)
  207.     ystretch = 1; /* Not interlaced */
  208.   else if (scr->Height <= 700)
  209.     ystretch = 2; /* Productivity or interlaced */
  210.   else
  211.     ystretch = 4; /* Interlaced Productivity or A2024 */
  212.  
  213.   if (has_obsta)
  214.     title = getcatalogstr(catalog, MSG_SCRTITLE1, MSG_SCRTITLE1_STR);
  215.   else
  216.     title = getcatalogstr(catalog, MSG_SCRTITLE2, MSG_SCRTITLE2_STR);
  217.  
  218.   win = OpenWindowTags(NULL,
  219.                WA_InnerWidth, XSIZE*BSIZE*xstretch+2*xstretch,
  220.                WA_InnerHeight, YSIZE*BSIZE*ystretch+2*ystretch+1,
  221.                WA_IDCMP, CLOSEWINDOW | RAWKEY,
  222.                WA_ScreenTitle, title,
  223.                WA_Flags, ACTIVATE
  224.                | WINDOWCLOSE
  225.                | WINDOWDRAG
  226.                | RMBTRAP
  227.                | WINDOWDEPTH
  228.                | SMART_REFRESH
  229.                | NOCAREREFRESH,
  230.                TAG_DONE);
  231.  
  232.   UnlockPubScreen(NULL, scr);
  233.  
  234.   if (!win)
  235.     exit_prog(20);
  236.   rp = win->RPort;
  237.   border_left = win->BorderLeft + xstretch;
  238.   border_top = win->BorderTop + ystretch;
  239. }
  240.  
  241.  
  242. void close_window(void)
  243. {
  244.   CloseWindow(win);
  245. }
  246.  
  247.  
  248. void notify(char *str)
  249. {
  250.   struct EasyStruct es;
  251.  
  252.   es.es_StructSize = sizeof(struct EasyStruct);
  253.   es.es_Flags = 0;
  254.   es.es_Title = getcatalogstr(catalog,
  255.                   MSG_EASYREQ_RESULT_TITLE,
  256.                   MSG_EASYREQ_RESULT_TITLE_STR);
  257.   es.es_TextFormat = str;
  258.   es.es_GadgetFormat = getcatalogstr(catalog,
  259.                      MSG_EASYREQ_RESULT_GAD,
  260.                      MSG_EASYREQ_RESULT_GAD_STR);
  261.   EasyRequestArgs(NULL, &es, NULL, NULL);
  262. }
  263.  
  264.  
  265. #define bleft (border_left + (x * BSIZE * xstretch))
  266. #define bwidth (border_left + (x * BSIZE * xstretch) + BSIZE * xstretch - 1)
  267. #define btop (border_top + (y * BSIZE * ystretch))
  268. #define bheight (border_top + (y * BSIZE * ystretch) + BSIZE * ystretch - 1)
  269.  
  270. void set_point(int x, int y, int item)
  271. {
  272.   grid[x][y] = item;
  273.  
  274.   SetAPen(rp, item);
  275.   RectFill(rp, bleft, btop, bwidth, bheight);
  276. }
  277.  
  278.  
  279. void clear_grid(void)
  280. {
  281.   int x, y;
  282.  
  283.   for (x = 0; x < XSIZE; x++)
  284.     for (y = 0; y < YSIZE; y++)
  285.       grid[x][y] = CLEAR;
  286.   SetAPen(rp, CLEAR);
  287.   RectFill(rp,
  288.        border_left, border_top,
  289.        border_left + XSIZE * BSIZE * xstretch,
  290.        border_top + YSIZE * BSIZE * ystretch);
  291. }
  292.  
  293.  
  294. void draw_score(void)
  295. {
  296.   static char str[20];
  297.   char *fmtstr;
  298.  
  299.   fmtstr = getcatalogstr(catalog, MSG_WINDOWTITLE, MSG_WINDOWTITLE_STR);
  300.   sprintf(str, fmtstr, length);
  301.   SetWindowTitles(win, str, (APTR) -1);
  302. }
  303.  
  304.  
  305. void pause(void)
  306. {
  307.   struct IntuiMessage *msg;
  308.  
  309.   for (;;) {
  310.     msg = (struct IntuiMessage *) WaitPort(win->UserPort);
  311.     if (msg->Class == RAWKEY && msg->Code & 0x80)
  312.       ReplyMsg(GetMsg(win->UserPort));
  313.     else
  314.       return;
  315.   }
  316. }
  317.  
  318.  
  319. void create_frog(int n)
  320. {
  321.   int x, y;
  322.  
  323.   do {
  324.     x = RangeRand(XSIZE);
  325.     y = RangeRand(YSIZE);
  326.   } while (grid[x][y] != CLEAR);
  327.   frx[n] = x;
  328.   fry[n] = y;
  329.   set_point(x, y, FROG);
  330.   frogtime[n] = 20 + RangeRand(50);
  331. }
  332.  
  333.  
  334. void check_frogs(void)
  335. {
  336.   int n, x, y;
  337.  
  338.   for (n = 0; n < NFROGS; n++) {
  339.     if (!frogtime[n]--) {
  340.       x = frx[n];
  341.       y = fry[n];
  342.       create_frog(n);
  343.       if (has_obsta)
  344.     set_point(x, y, RangeRand(10) ? CLEAR : BRICK);
  345.       else
  346.     set_point(x, y, CLEAR);
  347.     }
  348.   }
  349. }
  350.  
  351.  
  352. void replace_frog(int x, int y)
  353. {
  354.   int n;
  355.  
  356.   for (n = 0; n < NFROGS; n++) {
  357.     if (frx[n] == x && fry[n] == y)
  358.       create_frog(n);
  359.   }
  360.   set_point(x, y, CLEAR);
  361. }
  362.  
  363.  
  364. void setup_game(void)
  365. {
  366.   int i;
  367.  
  368.   clear_grid();
  369.   length = 1;
  370.   draw_score();
  371.   head = tail = 0;
  372.   snx[0] = XSIZE / 2;
  373.   sny[0] = YSIZE / 2;
  374.   set_point(snx[0], sny[0], SNAKE);
  375.   key = KEY_RIGHT;
  376.   for (i = 0; i < NFROGS; i++)
  377.     create_frog(i);
  378.   draw_score();
  379. }
  380.  
  381.  
  382. BOOL play_one_game(void)
  383. {
  384.   int item;
  385.   struct IntuiMessage *msg;
  386.   ULONG class;
  387.   USHORT code;
  388.   int x, y, nx, ny;
  389.  
  390.   for (;;) {
  391.     Delay(4L);
  392.     WaitTOF();
  393.     while (msg = (struct IntuiMessage *) GetMsg(win->UserPort)) {
  394.       class = msg->Class;
  395.       code = msg->Code;
  396.       ReplyMsg((struct Message *) msg);
  397.       switch (class) {
  398.       case CLOSEWINDOW:
  399.     return (TRUE);
  400.       case RAWKEY:
  401.     if (!(code & 0x80))
  402.       switch (code) {
  403.       case KEY_ESC:
  404.         return TRUE;
  405.       case KEY_Pause:
  406.         pause();
  407.         break;
  408.       case KEY_UP:
  409.       case KEY_DOWN:
  410.       case KEY_LEFT:
  411.       case KEY_RIGHT:
  412.         key = code;
  413.       }
  414.       }
  415.     }
  416.  
  417.     x = snx[head];
  418.     y = sny[head];
  419.     switch (key) {
  420.     case KEY_UP:
  421.       nx = x;
  422.       ny = y - 1;
  423.       break;
  424.     case KEY_DOWN:
  425.       nx = x;
  426.       ny = y + 1;
  427.       break;
  428.     case KEY_LEFT:
  429.       nx = x - 1;
  430.       ny = y;
  431.       break;
  432.     case KEY_RIGHT:
  433.       nx = x + 1;
  434.       ny = y;
  435.       break;
  436.     }
  437.     if (nx < 0 || ny < 0 || nx >= XSIZE || ny >= YSIZE)
  438.       return FALSE;
  439.     item = grid[nx][ny];
  440.     switch (item) {
  441.     case FROG:
  442.       grow += 2 + RangeRand(10);
  443.       replace_frog(nx, ny);
  444.     case CLEAR:
  445.       head = (head + 1) % 1500;
  446.       snx[head] = nx;
  447.       sny[head] = ny;
  448.       set_point(nx, ny, SNAKE);
  449.       if (grow) {
  450.     grow--;
  451.     length++;
  452.     draw_score();
  453.       } else {
  454.     set_point(snx[tail], sny[tail], CLEAR);
  455.     tail = (tail + 1) % 1500;
  456.       }
  457.       break;
  458.     case SNAKE:
  459.     case BRICK:
  460.       return (FALSE);
  461.     }
  462.     check_frogs();
  463.   }
  464. }
  465.  
  466.  
  467. void play_game(void)
  468. {
  469.   char str[80], *fmtstr;
  470.  
  471.   for (;;) {
  472.     setup_game();
  473.     WaitPort(win->UserPort);
  474.     if (play_one_game())
  475.       return;
  476.     fmtstr = getcatalogstr(catalog,
  477.                MSG_EASYREQ_RESULT,
  478.                MSG_EASYREQ_RESULT_STR);
  479.     sprintf(str, fmtstr, length);
  480.     notify(str);
  481.   }
  482. }
  483.  
  484.  
  485. void randomize(void)
  486. {
  487.   extern ULONG __far RangeSeed;
  488.   time_t t;
  489.  
  490.   time(&t);
  491.   RangeSeed = (ULONG) t;
  492. }
  493.  
  494.  
  495. char *getcatalogstr(struct Catalog *catalog, LONG strNum, char *defStr)
  496. {
  497.   if (LocaleBase)
  498.     return (char *) GetCatalogStr(catalog, strNum, defStr);
  499.   else
  500.     return defStr;
  501. }
  502.  
  503.  
  504. void main(void)
  505. {
  506.   BYTE old_pri;
  507.   struct TagItem OC_tags[2];
  508.  
  509.   OC_tags[0].ti_Tag = OC_BuiltInLanguage;
  510.   OC_tags[0].ti_Data = (ULONG) "dansk";
  511.   OC_tags[1].ti_Tag = TAG_DONE;
  512.   OC_tags[1].ti_Data = NULL;
  513.  
  514.   if (IntuitionBase = OpenLibrary("intuition.library", 37L))
  515.     if (GfxBase = OpenLibrary("graphics.library", 37L)) {
  516.       LocaleBase = (struct LocaleBase *) OpenLibrary("locale.library", 38L);
  517.       if (LocaleBase)
  518.     catalog = OpenCatalogA(NULL, "Orm.catalog", OC_tags);
  519.       randomize();
  520.       open_window();
  521.       old_pri = SetTaskPri(FindTask(0), PRIORITY);
  522.       play_game();
  523.       close_window();
  524.       SetTaskPri(FindTask(0), old_pri);
  525.       if (LocaleBase)
  526.     CloseCatalog(catalog);
  527.       }
  528.   exit_prog(0);
  529. }
  530. @
  531.  
  532.  
  533. 1.3
  534. log
  535. @Localized. Defaults to builtin Danish strings if
  536. locale.library isn't available; thus, it still runs
  537. under AmigaDOS 2.04 (V37).
  538. @
  539. text
  540. @d16 5
  541. d33 2
  542. a34 2
  543. char RCSid[] = "$Id: Orm.c,v 1.2 93/10/12 18:03:27 Torsten Exp Locker: Torsten $";
  544. char version[] = "$VER: Orm 1.3 (19.10.93)";
  545. d93 1
  546. a93 1
  547. #define KEY_P     25
  548. d366 1
  549. a366 1
  550.       case KEY_P:
  551. @
  552.  
  553.  
  554. 1.2
  555. log
  556. @Added function prototypes.
  557. Can be compiled with either DICE or SAS/C 5.10.
  558. Tries to be smart about the screen display mode.
  559. Obstacles disabled on two-colour screens.  
  560. Knows about window border sizes.
  561. @
  562. text
  563. @d16 7
  564. d28 2
  565. a29 2
  566. char RCSid[] = "$Id: Orm.c,v 1.1 93/10/12 00:04:41 Torsten Exp Locker: Torsten $";
  567. char version[] = "$VER: Orm 1.2 (12.10.93)";
  568. d34 1
  569. d39 1
  570. d44 1
  571. d63 1
  572. a75 2
  573. #define SCR_TITLE1 "Orm til Amiga - 1993"
  574. #define SCR_TITLE2 "Orm til Amiga - 1993 (uden forhindringer)"
  575. d110 1
  576. a110 1
  577.  
  578. d112 3
  579. a114 1
  580. extern APTR IntuitionBase, GfxBase;
  581. d129 1
  582. d140 1
  583. d169 5
  584. d178 1
  585. a178 1
  586.                WA_ScreenTitle, (has_obsta?SCR_TITLE1:SCR_TITLE2),
  587. d210 3
  588. a212 1
  589.   es.es_Title = "Resultat";
  590. d214 3
  591. a216 1
  592.   es.es_GadgetFormat = "OK";
  593. d253 1
  594. d255 2
  595. a256 1
  596.   sprintf(str, "Længde: %d", length);
  597. d425 1
  598. a425 1
  599.   char str[80];
  600. d432 4
  601. a435 1
  602.     sprintf(str, "Ormen blev %d led lang", length);
  603. d451 9
  604. d463 1
  605. d465 5
  606. d472 3
  607. d481 3
  608. a483 1
  609.     }
  610. @
  611.  
  612.  
  613. 1.1
  614. log
  615. @This is my first version of Michael's game.
  616. @
  617. text
  618. @d15 4
  619. a18 1
  620.  * $Log$
  621. d21 4
  622. a26 1
  623. #include <graphics/gfx.h>
  624. d29 1
  625. d31 10
  626. a40 1
  627. #include <clib/alib_protos.h>
  628. d42 7
  629. d52 1
  630. d55 6
  631. a60 1
  632. #else
  633. d62 1
  634. d65 2
  635. a66 5
  636. char RCSid[] = "$Id$";
  637. char version[] = "$VER: Orm 1.1 (11.10.93)";
  638. char copyright[] = "$COPYRIGHT:*PUBLIC DOMAIN* - M. Warner & T. Poulin$";
  639.  
  640. #define SCR_TITLE "Orm til Amiga - 1993"
  641. d69 1
  642. d85 18
  643. a103 1
  644.  
  645. d106 9
  646. a114 1
  647. BOOL stretch;
  648. d127 9
  649. d137 18
  650. a154 3
  651.   scr = LockPubScreen(NULL);
  652.   stretch = scr->Height >= 400; /* this is a hack */
  653.   UnlockPubScreen(NULL, scr);
  654. d157 2
  655. a158 2
  656.                WA_Width, 251,
  657.                WA_Height, stretch ? 256 : 135,
  658. d160 1
  659. a160 1
  660.                WA_ScreenTitle, SCR_TITLE,
  661. d169 3
  662. d173 1
  663. a173 1
  664.     exit_prog(10);
  665. d175 2
  666. d192 1
  667. a192 1
  668.   es.es_Title = "Orm: Resultat";
  669. d199 4
  670. a202 4
  671. UBYTE grid[XSIZE][YSIZE];
  672. UBYTE snx[1500], sny[1500], frx[NFROGS], fry[NFROGS];
  673. int dir, length, head, tail, grow, frogtime[NFROGS];
  674. UBYTE key;
  675. d209 1
  676. a209 4
  677.   if (stretch)
  678.     RectFill(rp, 5 + x * 6, 12 + y * 6, 5 + x * 6 + 5, 12 + y * 6 + 5);
  679.   else
  680.     RectFill(rp, 5 + x * 6, 12 + y * 3, 5 + x * 6 + 5, 12 + y * 3 + 2);
  681. d221 4
  682. a224 1
  683.   RectFill(rp, 5, 12, 5 + XSIZE * 6, 12 + YSIZE * (stretch ? 6 : 3));
  684. d275 4
  685. a278 1
  686.       set_point(x, y, RangeRand(10) ? CLEAR : BRICK);
  687. d408 1
  688. a408 1
  689.     sprintf(str, "Du blev %d led lang", length);
  690. d417 1
  691. a417 1
  692.   int clk[2];
  693. d419 2
  694. a420 2
  695.   timer(clk);
  696.   RangeSeed = clk[0] + clk[1];
  697. @
  698.